package com.sanshow.charging.adapter;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanshow.charging.MyApplication;
import com.sanshow.charging.R;
import com.sanshow.charging.data.DownCommand;
import com.sanshow.charging.model.Table1Model;
import com.sanshow.charging.model.Table2Model;

import java.util.concurrent.BlockingQueue;

import me.f1reking.serialportlib.SerialPortHelper;

public class Table2Adapter extends BaseQuickAdapter<Table2Model.ResultBean.ListBean, BaseViewHolder> {

    /**
     * 增加一个构造方法，便于没有数据时候初始化适配器
     */
    public Table2Adapter() {
        super(R.layout.item_table2);
    }

    /**
     * 继承BaseQuickAdapter后需要重写的方法
     *
     * @param helper view持有者，为重用view而设计，减少每次创建view的内存消耗
     * @param data   我们的列表数据
     */
    @SuppressLint("ResourceAsColor")
    @Override
    protected void convert(@NonNull BaseViewHolder helper, Table2Model.ResultBean.ListBean data) {
        helper.setText(R.id.table2_column1, data.getColumn1())
                .setText(R.id.table2_column2, data.getColumn2())
                .setText(R.id.table2_column3, data.getColumn3())
                .setText(R.id.table2_column4,data.getColumn4())
                .setText(R.id.table2_column5,data.getColumn5());


        if(data.getColumn3().equals("开")){
            helper.setTextColor(R.id.table2_column3,MyApplication.getContext().getResources().getColor(R.color.tbl_normal));
        }else{
            helper.setTextColor(R.id.table2_column3,MyApplication.getContext().getResources().getColor(R.color.tv_4A4A4A));
        }

        if(data.getColumn4().equals("不可用")){
            helper.setTextColor(R.id.table2_column4,MyApplication.getContext().getResources().getColor(R.color.tbl_normal));
        }else{
            helper.setTextColor(R.id.table2_column4,MyApplication.getContext().getResources().getColor(R.color.tv_4A4A4A));
        }

        Button button = helper.getView(R.id.table2_column6);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                SerialPortHelper serialPortHelper = MyApplication.getSerialPortHelper();
                if (serialPortHelper != null) {
                    MyApplication.setBackOpenDoorTaskQueue(DownCommand.getItemCodeForHex(data.getColumn1()));
                    //serialPortHelper.sendHex(DownCommand.getOpenDoorCmdHexForLocal(data.getColumn1()));
                    /*Toast toast = Toast.makeText(MyApplication.getContext(), "【"+data.getColumn1()+"】开门指令已发送", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();*/
                }else{
                    Toast toast = Toast.makeText(MyApplication.getContext(), "连接已断开", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            }
        });
    }

}
